<template>
  <div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      stripe
      row-key="id"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        reserve-selection
      ></el-table-column>
      <el-table-column prop="orderBy" label="排序" width="50">
      </el-table-column>
      <el-table-column prop="title" label="文章标题" show-overflow-tooltip>
        <template slot-scope="{ row }">
          <span class="cup" @click="handleEdit(row)">{{ row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="author"
        label="作者"
        width="100"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">
          <span>{{ row.author || '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="clickCount"
        label="点击数"
        width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="collect"
        label="数据来源"
        width="100"
        align="center"
      >
        <template slot-scope="{ row }">
          <span>
            {{ row.collect && row.collect == 'Y' ? '数据采集' : '默认来源' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="发布状态" width="100">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatus(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button type="success" size="mini" @click="handlePreview(row)"
              >预览</el-button
            >
            <el-button type="primary" size="mini" @click="handleEdit(row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="handleDelete(row)"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
export default {
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        classifyId: '',
      },
      tableData: [],
      pageObj: {},
    }
  },
  props: ['classifyId'],
  mounted() {
    this.getList()
    this.$bus.$on('refreshNewsContentsList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    this.$bus.$off('refreshNewsContentsList')
  },
  watch: {
    classifyId: {
      handler(v) {
        this.listQuery.classifyId = v
        this.getList()
        this.$refs.multipleTable.clearSelection()
      },
    },
  },
  methods: {
    async getList() {
      if (!this.listQuery.classifyId) return
      let res = await this.$store.dispatch('queryNewsContents', this.listQuery)
      this.tableData = res.data
      this.pageObj = res
    },
    //编辑
    handleEdit(row) {
      this.$bus.$emit('showDialogNewsContentsAdd', row)
    },
    //预览
    handlePreview(row) {
      this.$bus.$emit('collectLogPreview', row)
    },
    //批量操作
    handleSelectionChange(val) {
      this.$bus.$emit('deleteBatchNewsContents', val)
    },
    //发布
    async handleStatus(row) {
      await this.$store.dispatch('updateNewsContents', row)
      this.getList()
    },
    //删除
    handleDelete(row) {
      this.deleteTips(() => {
        this.delete(row)
      })
    },
    async delete(row) {
      await this.$store.dispatch('deleteNewsContents', row)
      this.getList()
    },
  },
}
</script>